//
//  TipsInputView.m
//  carcareIOS
//
//  Created by 王然 on 16/8/5.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "TipsInputView.h"

#define View_Width (__MAIN_WIDTH - 25 * 2 * __MAIN_RATIO_375)

#define View_Head_Y (49 * __MAIN_RATIO_375)
#define View_Head_Height (20)
#define View_HeadAndInputY (30 * __MAIN_RATIO_375)
#define View_Input_Height (50)
#define View_Btn_Y (40 * __MAIN_RATIO_375)
#define View_Btn_H (45)
#define View_Btn_Bottom (22)

#define View_Height_NoInput (View_Head_Y + View_Head_Height + View_HeadAndInputY + View_Btn_Y + View_Btn_H + View_Btn_Bottom)

@interface  TipsInputView()

@property (nonatomic , strong) UILabel * titleL;

@property (nonatomic , strong) UIView * inputBackV;

@property (nonatomic , strong) UIButton * sureBtn;

@property (nonatomic , strong) NSMutableArray * inputTFArr;

@property (nonatomic , strong) NSMutableArray * inputTextArr;

@property (nonatomic , copy) void (^sureBtnClick)(NSArray * inputTextArr);

@end

@implementation TipsInputView

- (instancetype)init
{
    self = [super initWithFrame:CGRectMake(0, 0, View_Width, View_Height_NoInput)];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self createUI];
    }
    return self;
}
- (void)createUI
{
    CGFloat viewX = 24 * __MAIN_RATIO_375;
    self.titleL = [[[UILabel labelWithFrame:CGRectMake(viewX, View_Head_Y , View_Width - viewX * 2, View_Head_Height) font:FONT_DEFAULT_Light(13) text:@"" textColor:COLOR_HEXSTRING(@"#9f9f9f")] setupOnView:self] labelResetTextAlignment:NSTextAlignmentCenter];
    
    self.inputBackV = [[UIView viewWithFrame:CGRectMake(viewX, self.titleL.bottom + View_HeadAndInputY, self.titleL.width, 10)] setupOnView:self]   ;
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture)];
    [self addGestureRecognizer:tapGesture];
    
     __weak __typeof(self) wself = self;
    self.sureBtn = [[[[UIButton buttonWithFrame:CGRectMake(viewX, self.inputBackV.bottom + View_Btn_Y, self.titleL.width, View_Btn_H) title:@"确定" textColor:[UIColor whiteColor] font:FONT_DEFAULT_Light(15) click:^{
        [wself sureBtnClickOn];
    }] setupOnView:self] resetBackgroundColor:[Theme mainNavColor]] resetCornerRadius:2];
    
    CGFloat btnY = 12;
    UIImage * cancelImage = ImageNamed(@"tipsViewCancelIcon");
    UIImageView * cancelIV = [[UIImageView imageViewWithCenter:CGPointMake(View_Width - btnY - cancelImage.size.width / 2, btnY + cancelImage.size.height / 2) image:cancelImage] setupOnView:self];
    CGFloat btnAddW = 20 * __MAIN_RATIO_375;
    [[UIButton buttonEmptyWithFrame:CGRectMake(cancelIV.left - btnAddW, cancelIV.top - btnAddW, btnAddW * 3, btnAddW * 3) click:^{
        [AlertMgr dismiss:TYPE_VIEW_ALERT];
    }] setupOnView:self];
}
- (void)tapGesture
{
    [self clickOnBG];
}
- (void)sureBtnClickOn
{
    [self clickOnBG];
    self.inputTextArr = nil;
    self.inputTextArr = [NSMutableArray arrayWithCapacity:self.inputTFArr.count];
    NSInteger nilCount = 0;
    for (UITextField * tf in self.inputTFArr) {
        if (![tf.text isNotNil]) {
            nilCount ++;
            [AlertMgr showShortTextTips:@[tf.placeholder]];
        }
    }
    if (nilCount == 0) {
        for (UITextField * tf in self.inputTFArr) {
            
            [self.inputTextArr addObject:tf.text];
        }
        if (self.sureBtnClick) self.sureBtnClick(self.inputTextArr);
    }
}
- (void)reloadTitle:(NSString *)title withInputArray:(NSArray *)inputArr withClick:(void(^)(NSArray * inputTextArr))click
{
    if(click)self.sureBtnClick = click;
    [self.inputBackV removeAllSubviews];
    [self resetViewHeighWithCount:inputArr.count];
    
    self.titleL.text = title;
    self.inputTFArr = nil;
    self.inputTFArr = [NSMutableArray arrayWithCapacity:inputArr.count];
    
    for (int index = 0 ; index < inputArr.count; index ++) {
        
        UITextField * textFd = [[[UITextField textFieldWithFrame:CGRectMake(0, (View_Input_Height - 1 )* index, self.inputBackV.width, View_Input_Height) textColor:COLOR_HEXSTRING(@"#bababa") font:FONT_DEFAULT_Light(14)] setupOnView:self.inputBackV] resetBorderWidth:1 borderColor:COLOR_HEXSTRING(@"#cbcbcb")];
        textFd.placeholder = inputArr[index];
        textFd.backgroundColor = [UIColor whiteColor];
        textFd.textAlignment = NSTextAlignmentCenter;
//        if (index != inputArr.count - 1) {
//            [[[UIView viewWithFrame:CGRectMake(textFd.left, textFd.bottom - 1, textFd.width, 1)] setupOnView:self.inputBackV] resetBackgroundColor:COLOR_HEXSTRING(@"#cbcbcb")];
//        }
        
        [self.inputTFArr addObject:textFd];
    }

}
- (void)resetViewHeighWithCount:(NSInteger)count
{
    
    [self.inputBackV setHeight:View_Input_Height * count];
    self.height = View_Height_NoInput + self.inputBackV.height;
    self.sureBtn.top = self.inputBackV.bottom + View_Btn_Y;
    
}
- (void)clickOnBG
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
@end
